﻿/*
 *  PassBag is multi-platform software that securely manage/store passwords
 *  Copyright (C) <2009>  <Hugo Rodrigues PEREIRA>
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *	
 *  You can find a copy of the GPLv3 at project <root directory>/gpl.txt
 *
 */

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using PassBag.Logic.Settings;
using PassBag.Logic.Backup;

namespace PassBag.Presentation.Backup {
    public partial class BackupSetRestoreDialog : PassBag.Presentation.Backup.BackupSetDialog {
        public BackupSetRestoreDialog () {
            InitializeComponent ();

            LoadRessource ();
        }

        #region Load Resource

        /// <summary>
        /// Loads the ressource.
        /// </summary>
        private void LoadRessource () {
            this.Text = Properties.Resources.BackupSetRestoreDialog_Text;
            dialogTitleLabel.Text = Properties.Resources.BackupSetRestoreDialog_Title;
        }

        #endregion

        private void restoreButton_Click (object sender, EventArgs e) {
            DialogResult = DialogResult.Cancel;

            if (fileState == BackupFileState.FileExist) {
                object fileName = SelectFile (Properties.Resources.BackupSetRestoreDialog_SelectFileTitle);

                if (fileName.GetType() != typeof(DialogResult) && ((DialogResult)fileName) != DialogResult.Cancel) {

                    if (!CurrentPasswordFile.Equals (fileName.ToString (), StringComparison.InvariantCultureIgnoreCase)) {
                        if (System.IO.Directory.Exists (BackupSet.GetFilePath (fileName.ToString ()))) {
                            if (backupSetListBox.SelectedIndex != -1) { // if and item is selected

                                if (PassBag.Logic.Backup.BackupSet.ExtractZipEntry (backupSetTextBox.Text, ((BackupZipEntry)backupSetListBox.SelectedItem).FileName, fileName.ToString ())) {
                                    DialogResult = DialogResult.OK;
                                    this.Close ();
                                }
                                else {
                                    MessageDialog.ShowError (this, Properties.Resources.BackupSetRestoreDialog_RestoreError);
                                }

                            }
                            else {
                                MessageDialog.ShowInformation (this, Properties.Resources.BackupSetRestoreDialog_SelectEntryFirstError);
                            }
                        }
                        else {
                            MessageDialog.ShowError (this, Properties.Resources.BackupSetRestoreDialog_MalformedPathError);
                        }
                    }
                    else {
                        MessageDialog.ShowInformation (this, Properties.Resources.BackupSetRestoreDialog_CanRestoreFileError);
                    }

                }
            }
        }

        public static object SelectFile (string title) {
            SaveFileDialog dlgSave = new SaveFileDialog ();

            if (string.IsNullOrEmpty (title)) {
                title = Properties.Resources.BackupSetRestoreDialog_CanRestoreFileError;
            }

            dlgSave.DefaultExt = XmlSettingsManager.Settings.BackupFileExtension;
            dlgSave.Filter = string.Format (Properties.Resources.Global_FileDialogFilter, XmlSettingsManager.Settings.BackupFileExtension);
            dlgSave.Title = title;
            dlgSave.FilterIndex = 1;

            //show dialog and check result
            if (dlgSave.ShowDialog () == DialogResult.OK) {
                return dlgSave.FileName;
            }
            else {
                return DialogResult.Cancel;
            }
        }
        
        public new DialogResult ShowDialog () {
            LoadBackupSetItems ();

            return base.ShowDialog ();
        }
    }

}
